package com.live.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.live.common.Constant;
import com.live.task.LiveMatch;
import com.live.task.LiveUpgrade;

public class LiveMatchActivity extends LiveBaseActivity {

	static final int DATE_DIALOG_ID = 0;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	private String today;

	private String matchCategory = "0";

	private String matchStatus = "全部";

	private ListView matchList = null;

	private Dialog statusDialog = null;

	private Dialog categoryDialog = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.live_match);

		TextView matchDate = (TextView) findViewById(R.id.matchDate);
		today = sdf.format(System.currentTimeMillis());
		matchDate.setText(today);

		matchList = (ListView) findViewById(R.id.matchList);

		SimpleAdapter adapter = new SimpleAdapter(context, liveMatchList, R.layout.match_list, new String[] {
				"matchName", "matchTime", "homeTeam", "homeTeamScore", "visitTeam", "visitTeamScore", "matchStatus",
				"matchRecord" }, new int[] { R.id.matchName, R.id.matchTime, R.id.homeTeam, R.id.homeTeamScore,
				R.id.visitTeam, R.id.visitTeamScore, R.id.matchStatus, R.id.matchRecord });

		matchList.setAdapter(adapter);
		matchList.setOnItemClickListener(new OnItemClickListener() {

			// 从当前画面到比分明细画面跳转，并传值。
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				Map<String, Object> sportsMatch = liveMatchList.get(arg2);

				Intent intent = new Intent();

				intent.setClass(context, LiveDetailActivity.class);

				// 实例化 Bundle，设置需要传递的参数
				Bundle bundle = new Bundle();
				bundle.putString("matchId", sportsMatch.get("matchId").toString());
				bundle.putString("matchOptaId", sportsMatch.get("matchOptaId").toString());
				bundle.putString("matchName", sportsMatch.get("matchName").toString());
				bundle.putString("matchCategory", sportsMatch.get("matchCategory").toString());
				bundle.putString("matchTime", today + " " + sportsMatch.get("matchTime").toString());
				bundle.putString("homeTeam", sportsMatch.get("homeTeam").toString());
				bundle.putString("homeTeamScore", sportsMatch.get("homeTeamScore").toString());
				bundle.putString("homeTeamPic", sportsMatch.get("homeTeamPic").toString());
				bundle.putString("visitTeam", sportsMatch.get("visitTeam").toString());
				bundle.putString("visitTeamScore", sportsMatch.get("visitTeamScore").toString());
				bundle.putString("visitTeamPic", sportsMatch.get("visitTeamPic").toString());
				bundle.putString("matchStatus", sportsMatch.get("matchStatus").toString());

				intent.putExtras(bundle);

				context.startActivity(intent);
			}
		});

		// 单击日期
		matchDate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});

		// 长按日期
		matchDate.setOnLongClickListener(new View.OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				today = sdf.format(new Date(System.currentTimeMillis()));
				TextView matchDate = (TextView) findViewById(R.id.matchDate);
				matchDate.setText(today);
				String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus=" + matchStatus
						+ "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
				return true;
			}
		});

		ImageView forwardDayBtn = (ImageView) findViewById(R.id.forwardDayBtn);
		forwardDayBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					Date date = sdf.parse(today);
					Calendar cal = Calendar.getInstance();
					cal.setTime(date);
					cal.add(5, -1);
					today = sdf.format(cal.getTime());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				TextView matchDate = (TextView) findViewById(R.id.matchDate);
				matchDate.setText(today);
				String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus=" + matchStatus
						+ "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
			}
		});

		ImageView nextDayBtn = (ImageView) findViewById(R.id.nextDayBtn);
		nextDayBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					Date date = sdf.parse(today);
					Calendar cal = Calendar.getInstance();
					cal.setTime(date);
					cal.add(5, 1);
					today = sdf.format(cal.getTime());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				TextView matchDate = (TextView) findViewById(R.id.matchDate);
				matchDate.setText(today);
				String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus=" + matchStatus
						+ "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
			}
		});

		TextView statusBtn = (TextView) findViewById(R.id.statusBtn);
		statusBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				statusDialog = new Dialog(context, R.style.dialog);
				// 去掉标题
				statusDialog.setContentView(R.layout.match_status);
				statusDialog.setCanceledOnTouchOutside(true);
				// 为获取屏幕宽、高
				Display d = getWindowManager().getDefaultDisplay();
				// 获取对话框当前的参数值
				LayoutParams p = statusDialog.getWindow().getAttributes();
				// 高度设置
				p.height = d.getHeight() / 6;
				// 宽度设置
				p.width = (int) (d.getWidth());
				p.y = d.getHeight() / 3 - 20;
				// 设置生效
				statusDialog.getWindow().setAttributes(p);
				// 显示对话框
				statusDialog.show();

				TextView matchALL = (TextView) statusDialog.findViewById(R.id.matchALL);
				matchALL.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {

						TextView statusBtn = (TextView) findViewById(R.id.statusBtn);

						TextView matchDate = (TextView) findViewById(R.id.matchDate);
						matchDate.setText(today);

						matchStatus = "全部";
						statusBtn.setText("赛况");

						String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus="
								+ matchStatus + "&matchCategory=" + matchCategory;
						LiveMatch task = new LiveMatch(context, matchList, today);
						task.execute(url);

						statusDialog.dismiss();
					}
				});

				TextView matchOver = (TextView) statusDialog.findViewById(R.id.matchOver);
				matchOver.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {

						TextView statusBtn = (TextView) findViewById(R.id.statusBtn);

						TextView matchDate = (TextView) findViewById(R.id.matchDate);
						matchDate.setText(today);

						matchStatus = "已结束";
						statusBtn.setText("已结束");

						String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus="
								+ matchStatus + "&matchCategory=" + matchCategory;
						LiveMatch task = new LiveMatch(context, matchList, today);
						task.execute(url);

						statusDialog.dismiss();
					}
				});

				TextView matchLiving = (TextView) statusDialog.findViewById(R.id.matchLiving);
				matchLiving.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {

						TextView statusBtn = (TextView) findViewById(R.id.statusBtn);

						TextView matchDate = (TextView) findViewById(R.id.matchDate);
						matchDate.setText(today);

						matchStatus = "直播中";
						statusBtn.setText("进行中");

						String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus="
								+ matchStatus + "&matchCategory=" + matchCategory;
						LiveMatch task = new LiveMatch(context, matchList, today);
						task.execute(url);

						statusDialog.dismiss();
					}
				});

				TextView matchWait = (TextView) statusDialog.findViewById(R.id.matchWait);
				matchWait.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {

						TextView statusBtn = (TextView) findViewById(R.id.statusBtn);

						TextView matchDate = (TextView) findViewById(R.id.matchDate);
						matchDate.setText(today);

						matchStatus = "未开始";
						statusBtn.setText("未开始");

						String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus="
								+ matchStatus + "&matchCategory=" + matchCategory;
						LiveMatch task = new LiveMatch(context, matchList, today);
						task.execute(url);

						statusDialog.dismiss();
					}
				});
			}
		});

		TextView refreshBtn = (TextView) findViewById(R.id.refreshBtn);
		refreshBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus=" + matchStatus
						+ "&matchCategory=" + matchCategory;
				LiveMatch task = new LiveMatch(context, matchList, today);
				task.execute(url);
			}
		});

		TextView categoryBtn = (TextView) findViewById(R.id.categoryBtn);
		categoryBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				categoryDialog = new Dialog(context, R.style.dialog);
				// 去掉标题
				categoryDialog.setContentView(R.layout.match_category);
				categoryDialog.setCanceledOnTouchOutside(true);
				// 为获取屏幕宽、高
				Display d = getWindowManager().getDefaultDisplay();
				// 获取对话框当前的参数值
				LayoutParams p = categoryDialog.getWindow().getAttributes();
				// 高度设置
				p.height = d.getHeight() / 6;
				// 宽度设置
				p.width = (int) (d.getWidth());
				p.y = d.getHeight() / 3 - 20;
				// 设置生效
				categoryDialog.getWindow().setAttributes(p);
				// 显示对话框
				categoryDialog.show();

				TextView matchALL = (TextView) categoryDialog.findViewById(R.id.matchALL);
				matchALL.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {

						TextView categoryBtn = (TextView) findViewById(R.id.categoryBtn);

						TextView matchDate = (TextView) findViewById(R.id.matchDate);
						matchDate.setText(today);

						matchCategory = "0";
						categoryBtn.setText("类别");

						String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus="
								+ matchStatus + "&matchCategory=" + matchCategory;
						LiveMatch task = new LiveMatch(context, matchList, today);
						task.execute(url);

						categoryDialog.dismiss();
					}
				});

				TextView matchBB = (TextView) categoryDialog.findViewById(R.id.matchBB);
				matchBB.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {

						TextView categoryBtn = (TextView) findViewById(R.id.categoryBtn);

						TextView matchDate = (TextView) findViewById(R.id.matchDate);
						matchDate.setText(today);

						matchCategory = "1";
						categoryBtn.setText("篮球");

						String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus="
								+ matchStatus + "&matchCategory=" + matchCategory;
						LiveMatch task = new LiveMatch(context, matchList, today);
						task.execute(url);

						categoryDialog.dismiss();
					}
				});

				TextView matchFB = (TextView) categoryDialog.findViewById(R.id.matchFB);
				matchFB.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						TextView categoryBtn = (TextView) findViewById(R.id.categoryBtn);

						TextView matchDate = (TextView) findViewById(R.id.matchDate);
						matchDate.setText(today);

						matchCategory = "2,3";
						categoryBtn.setText("足球");

						String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus="
								+ matchStatus + "&matchCategory=" + matchCategory;
						LiveMatch task = new LiveMatch(context, matchList, today);
						task.execute(url);

						categoryDialog.dismiss();
					}
				});

				TextView matchTB = (TextView) categoryDialog.findViewById(R.id.matchTB);
				matchTB.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {

						TextView categoryBtn = (TextView) findViewById(R.id.categoryBtn);

						TextView matchDate = (TextView) findViewById(R.id.matchDate);
						matchDate.setText(today);

						matchCategory = "5";
						categoryBtn.setText("台球");

						String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus="
								+ matchStatus + "&matchCategory=" + matchCategory;
						LiveMatch task = new LiveMatch(context, matchList, today);
						task.execute(url);

						categoryDialog.dismiss();
					}
				});

				TextView matchNB = (TextView) categoryDialog.findViewById(R.id.matchNB);
				matchNB.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						TextView categoryBtn = (TextView) findViewById(R.id.categoryBtn);

						TextView matchDate = (TextView) findViewById(R.id.matchDate);
						matchDate.setText(today);

						matchCategory = "6";
						categoryBtn.setText("网球");

						String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchStatus="
								+ matchStatus + "&matchCategory=" + matchCategory;
						LiveMatch task = new LiveMatch(context, matchList, today);
						task.execute(url);

						categoryDialog.dismiss();
					}
				});

			}
		});

		// 升级检测
		LiveUpgrade liveUpgradeTask = new LiveUpgrade(context);
		liveUpgradeTask.execute();

	}

	// 设置要检索的日期
	private DatePickerDialog.OnDateSetListener mSetDateListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int month, int day) {
			Calendar cal = Calendar.getInstance();
			cal.set(year, month, day);
			today = sdf.format(cal.getTime());

			TextView matchDate = (TextView) findViewById(R.id.matchDate);
			matchDate.setText(today);
			String url = Constant.SITE_URL + "live_match.php?matchTime=" + today + "&matchCategory=" + matchCategory;

			LiveMatch task = new LiveMatch(context, matchList, today);
			task.execute(url);
		}
	};

	protected Dialog onCreateDialog(int id) {
		Calendar c = Calendar.getInstance();
		switch (id) {
		case 0:
			return new DatePickerDialog(this, this.mSetDateListener, c.get(1), c.get(2), c.get(5));
		}
		return null;
	}

	public void onConfigurationChanged(Configuration config) {
		super.onConfigurationChanged(config);
	}

}